package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.domain.UserExamPaper;
import top.fengyulou.xiaofeng.dto.*;
import top.fengyulou.xiaofeng.service.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 后台用户管理
 */
@Controller
public class AdminExamController {

    @Resource
    private ExamService examService;
    @Resource
    private ExamPaperService examPaperService;
    @Resource
    private ExamUserService examUserService;
    @Resource
    private UserExamQuestionService userExamQuestionService;
    @Resource
    private UserExamPaperService userExamPaperService;

    /**
     * 列表页面
     */
    @RequestMapping("/admin/exam/list")
    public String list() {
        return "admin/exam/admin-exam-list";
    }

    /**
     * 添加页面
     */
    @RequestMapping("/admin/exam/save")
    public String savePage(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            ExamDto examDto = examService.getById(id);
            model.addAttribute("data", examDto);
        }
        return "admin/exam/admin-exam-save";
    }

    //列表
    @RequestMapping("/admin/exam/ajax/list")
    @ResponseBody
    public Jr list(@RequestBody ExamDto examDto) {
        examService.list(examDto);
        return Jr.yesd(examDto);
    }

    //保存
    @RequestMapping("/admin/exam/ajax/save")
    @ResponseBody
    public Jr save(HttpServletRequest request, ExamDto examDto) {
        examDto.setCurrentUserId(UserUtils.getUserId(request));
        examService.save(examDto);
        return Jr.yesd(examDto);
    }

    //删除
    @RequestMapping("/admin/exam/ajax/delete")
    @ResponseBody
    public Jr delete(@RequestParam String[] ids) {
        examService.delete(ids);
        return Jr.yes();
    }

    //考试管理页面
    @RequestMapping("/admin/exam/manage/{id}")
    public String manage(Model model, @PathVariable String id) {
        //考试信息
        ExamDto byId = examService.getById(id);
        model.addAttribute("exam", byId);
        //试卷信息
        List<String> paperIdListByExamId = examPaperService.getPaperIdListByExamId(id);
        String paperIds = String.join(",", paperIdListByExamId);
        model.addAttribute("paperIds", paperIds);
        //用户信息
        List<String> userIdListByExamId = examUserService.getUserIdListByExamId(id);
        String userIds = String.join(",", userIdListByExamId);
        model.addAttribute("userIds", userIds);
        return "admin/exam/admin-exam-manage";
    }

    //考试数据页面
    @RequestMapping("/admin/exam/data/{id}")
    public String examData(Model model, @PathVariable String id) {
        //考试信息
        ExamDto byId = examService.getById(id);
        model.addAttribute("exam", byId);
        model.addAttribute("examId", id);
        return "admin/exam/admin-exam-data";
    }

    //数据详情页面
    @RequestMapping("/admin/exam/data/info/{examId}/{userId}")
    public String examDataInfo(Model model, @PathVariable String examId,@PathVariable String userId) {
        //考试信息
        ExamDto byId = examService.getById(examId);
        model.addAttribute("exam", byId);
        model.addAttribute("examId", examId);
        model.addAttribute("userId", userId);
        return "admin/exam/admin-exam-data-info";
    }

    //考试阅卷页面
    @RequestMapping("/admin/exam/marking/{id}")
    public String examMarking(Model model, @PathVariable String id) {
        //考试信息
        ExamDto byId = examService.getById(id);
        model.addAttribute("exam", byId);
        //考试用户数量
        long userNum = examUserService.countByExamId(id);
        model.addAttribute("userNum",userNum);
        return "admin/exam/admin-exam-marking";
    }

    //考试阅卷页面
    @RequestMapping("/admin/exam/marking/paper/{examId}/{paperId}")
    public String examMarkingPaper(Model model, @PathVariable String examId,@PathVariable String paperId) {
        //考试信息
        ExamDto byId = examService.getById(examId);
        model.addAttribute("exam", byId);
        model.addAttribute("paperId", paperId);
        return "admin/exam/admin-exam-marking-paper";
    }

    //考试阅卷页面
    @RequestMapping("/admin/exam/marking/paper/info/{examId}/{paperId}/{userPaperId}")
    public String examMarkingPaperInfo(Model model, @PathVariable String examId,@PathVariable String paperId,@PathVariable String userPaperId) {
        //考试信息
        ExamDto byId = examService.getById(examId);
        model.addAttribute("exam", byId);
        model.addAttribute("paperId", paperId);
        model.addAttribute("examId", examId);

        UserExamQuestionDto userExamQuestionDto = new UserExamQuestionDto();
        userExamQuestionDto.setUserPaperId(userPaperId);
        List<QuestionDto> list = userExamQuestionService.list(userExamQuestionDto);
        Map<String, List<QuestionDto>> groupBy = list.stream().collect(Collectors.groupingBy(QuestionDto::getType));
        model.addAttribute("qstType", groupBy);
        model.addAttribute("userPaperId",userPaperId);
        return "admin/exam/admin-exam-marking-paper-info";
    }

    //修改试题分数
    @RequestMapping("/admin/exam/ajax/updateQstScore")
    @ResponseBody
    public Jr ajaxUpdateQstScore(@RequestParam String userPaperId,@RequestParam String userQstId,@RequestParam Integer score,HttpServletRequest request){
        if (score<0){
            return Jr.nom("不能为负数");
        }
        UserExamQuestionDto userExamQuestionDto = new UserExamQuestionDto();
        userExamQuestionDto.setId(userQstId);
        userExamQuestionDto.setUserScore(score);
        userExamQuestionDto.setCurrentUserId(UserUtils.getUserId(request));
        userExamQuestionDto.setUserPaperId(userPaperId);
        userExamQuestionService.updateScore(userExamQuestionDto);
        return Jr.yes();
    }

    //修改试题分数
    @RequestMapping("/admin/exam/ajax/updateQstScore2")
    @ResponseBody
    public Jr ajaxUpdateQstScore2(@RequestParam String userPaperId,@RequestParam String scoreContent,@RequestParam String examId,@RequestParam String paperId,HttpServletRequest request){
        String[] qstTextArr = scoreContent.split(",");
        for (String qstText : qstTextArr) {
            String[] scoreArr = qstText.split("\\|");
            int score = Integer.parseInt(scoreArr[0]);
            if (score<0){
                continue;
            }
            String userQstId = scoreArr[1];
            UserExamQuestionDto userExamQuestionDto = new UserExamQuestionDto();
            userExamQuestionDto.setId(userQstId);
            userExamQuestionDto.setUserScore(score);
            userExamQuestionDto.setCurrentUserId(UserUtils.getUserId(request));
            userExamQuestionDto.setUserPaperId(userPaperId);
            userExamQuestionService.updateScore(userExamQuestionDto);
        }
        //修改阅卷状态
        UserExamPaperDto userExamPaperDto = new UserExamPaperDto();
        userExamPaperDto.setId(userPaperId);
        userExamPaperDto.setMarkingStatus(1);
        userExamPaperDto.setExamId(examId);
        userExamPaperDto.setPaperId(paperId);
        userExamPaperDto.setCurrentUserId(UserUtils.getUserId(request));
        userExamPaperService.updateMarkingStatus(userExamPaperDto);
        return Jr.yes();
    }

    /**
     * 设置用户
     *
     * @return
     */
    @RequestMapping("/admin/exam/user/{examId}")
    public String user(@PathVariable String examId, Model model) {
        model.addAttribute("examId", examId);
        return "admin/exam/admin-exam-user";
    }

    /**
     * 设置试卷
     *
     * @return
     */
    @RequestMapping("/admin/exam/paper/{examId}")
    public String paper(@PathVariable String examId, Model model) {
        model.addAttribute("examId", examId);
        return "admin/exam/admin-exam-paper";
    }

    //选择用户页面
    @RequestMapping("/admin/exam/select/user")
    public String selectUser() {
        return "admin/exam/admin-exam-select-user";
    }

    //选择试卷页面
    @RequestMapping("/admin/exam/select/paper")
    public String selectPaper() {
        return "admin/exam/admin-exam-select-paper";
    }

    /**
     * 用户列表查询
     */
    @RequestMapping("/admin/exam/ajax/user/list")
    @ResponseBody
    public Jr userList(@RequestBody UserDto userDto) {
        examService.userListPage(userDto);
        return Jr.yesd(userDto);
    }

    /**
     * 试卷列表查询
     */
    @RequestMapping("/admin/exam/ajax/paper/list")
    @ResponseBody
    public Jr paperList(@RequestBody PaperDto paperDto) {
        examService.paperListPage(paperDto);
        return Jr.yesd(paperDto);
    }

    /**
     * 试卷列表查询
     */
    @RequestMapping("/admin/exam/ajax/paper/marking/list")
    @ResponseBody
    public Jr paperMarkingList(@RequestBody PaperDto paperDto) {
        examService.paperMarkingListPage(paperDto);
        return Jr.yesd(paperDto);
    }

    /**
     * 用户试卷列表查询
     */
    @RequestMapping("/admin/exam/ajax/user/paper/list")
    @ResponseBody
    public Jr userPaperList(@RequestBody PaperDto paperDto) {
        examPaperService.userPaperListPage(paperDto);
        return Jr.yesd(paperDto);
    }

    //添加考试用户
    @RequestMapping("/admin/exam/ajax/save/user")
    @ResponseBody
    public Jr saveUser(HttpServletRequest request, ExamDto examDto) {
        examDto.setCreateUserId(UserUtils.getUserId(request));
        examDto.setCreateTime(new Date());
        examService.saveUser(examDto);
        List<String> userIdListByExamId = examUserService.getUserIdListByExamId(examDto.getId());
        String userIds = String.join(",", userIdListByExamId);
        return Jr.yesd(userIds);
    }

    //添加考试试卷
    @RequestMapping("/admin/exam/ajax/save/paper")
    @ResponseBody
    public Jr savePaper(HttpServletRequest request, ExamDto examDto) {
        examDto.setCreateUserId(UserUtils.getUserId(request));
        examService.savePaper(examDto);
        //查询最新的数据
        List<String> paperIdListByExamId = examPaperService.getPaperIdListByExamId(examDto.getId());
        String paperIds = String.join(",", paperIdListByExamId);
        return Jr.yesd(paperIds);
    }

    //保存试卷
    @RequestMapping("/admin/exam/ajax/paper/delete")
    @ResponseBody
    public Jr deletePaper(@RequestParam String[] ids) {
        examPaperService.delete(ids);
        return Jr.yes();
    }

    //保存试卷
    @RequestMapping("/admin/exam/ajax/user/delete")
    @ResponseBody
    public Jr deleteUser(@RequestParam String[] ids) {
        examUserService.delete(ids);
        return Jr.yes();
    }
}
